Skip to content

dataclasses.replace: fall through to typeshed sig#15962

Merged
hauntsaninja merged 3 commits intopython:masterfrom
ikonst:remove-_fail_not_dataclass
Aug 29, 2023
Merged

dataclasses.replace: fall through to typeshed sig#15962
hauntsaninja merged 3 commits intopython:masterfrom
ikonst:remove-_fail_not_dataclass

Conversation

@ikonst
Copy link
Copy Markdown
Contributor

@ikonst ikonst commented Aug 26, 2023

If the dataclasses plugin cannot determine a signature for dataclasses.replace, it should not report an error. The underlying typeshed signature will get a shot at verifying the type and reporting an error, and it would enable the following pattern (without typing replace's kwargs, though):

from dataclasses import is_dataclass, replace

def foo(x: object):
  if is_dataclass(x) and not isinstance(x, type):
#    ^^^^^^^^^^^^
#    this is a type-guard in the typeshed
#
    y = replace(x)

@github-actions

This comment has been minimized.

@ikonst ikonst force-pushed the remove-_fail_not_dataclass branch 2 times, most recently from e91d2da to a2f3780 Compare August 26, 2023 05:51
@ikonst ikonst force-pushed the remove-_fail_not_dataclass branch from a2f3780 to a657d10 Compare August 26, 2023 05:53
@github-actions

This comment has been minimized.

@ikonst ikonst force-pushed the remove-_fail_not_dataclass branch from 6fd543b to 0b95f3e Compare August 27, 2023 03:07
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, just one question :)


p = Person('John')
y = replace(p, name='Bob') # E: Argument 1 to "replace" has incompatible type "Person"; expected a dataclass
y = replace(p, name='Bob')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this can potentially introduce some new weak spots, but on other hand it can eventually work :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a false positive but it's due to #15974.

Comment thread test-data/unit/check-dataclasses.test Outdated
# error before type-guard
y = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object"
# no error after type-guard
if is_dataclass(x) and not isinstance(x, type):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to test just if is_dataclass(x):?

Copy link
Copy Markdown
Contributor Author

@ikonst ikonst Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because type is an object, and is_dataclass on an object either means it's a dataclass instance or type.

I'll improve the test to clarify that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

vision (https://github.com/pytorch/vision): typechecking got 1.37x slower (36.8s -> 50.5s)
(Performance measurements are based on a single noisy sample)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants